##################################################################
# File : /lua/maui/rings.lua
# Description : Rings control
# Author : GPG
##################################################################

local Control = import('control.lua').Control
local Group = import('group.lua').Group
local LazyVar = import('/lua/lazyvar.lua')

Rings = Class(Group) {
    __init = function(self, parent, name)
        Group.__init(self, parent, name or "Rings")
        self._items = {}
        self.NumPriorities = 0
        self.RingPriorities = {}
        self.RingRadius = {}
        self.Top:Set( function() parent.Top() end )
        self.Left:Set( function() parent.Left() end )
        self.Height:Set( function() parent.Height() end )
        self.Width:Set( function() parent.Width() end )
    end,

    ClearItems = function(self)
        if self._items then
            for i, v in self._items do
                if v then v:Destroy() v = false end
            end
        end
    end,
    
    AddItem = function(self, item, ringPriority)
        item.RingPriority = ringPriority
        if not self.RingPriorities[ringPriority] then
            self.RingPriorities[ringPriority] = {}
            self.NumPriorities = self.NumPriorities + 1
        end
        table.insert( self._items, item )
        table.insert( self.RingPriorities[ringPriority], item )
    end,    
    
    SetRingRadius = function(self, ringPri, radius)
        self.RingRadius[ringPri] = radius
    end,

    UpdatePosition = function(self, pos)
        local cam = GetCamera('WorldCamera')
        local rawZoom = 1-(cam:GetTargetZoom() / (cam:GetMaxZoom() - cam:GetMinZoom()))
        local curZoom = math.max(math.min(rawZoom, 1), .6)
        
        local numRings = 1
        local totalItems = 0
        local radiusAdd = 0
        for pri,itemTable in self.RingPriorities do
            local numItems = table.getn(itemTable)
            if not self.RingRadius[pri] then
                WARN('*UI WARNING: Using Default Ring radius for ring priority - ' .. pri)
            end
            local baseRadius = self.RingRadius[pri] or ( pri - 1 ) * 100

            local phi = (2*math.pi)/numItems
            local alpha = math.clamp((curZoom - 0.4) / 0.6,0.66,1.0)
            for i, v in itemTable do
                local index = i
                local control = v
                v.Height:Set(function() return curZoom * 64 end)
                v.Width:Set(function() return curZoom * 64 end)
                v.Left:Set(function() return (pos[1] - control.Width()) + ((curZoom*baseRadius)*math.cos(index*phi+(math.pi/2))) end)
                v.Top:Set(function() return (pos[2] - control.Height()) + ((curZoom*baseRadius)*math.sin(index*phi+(math.pi/2))) end)
                v.icon:SetAlpha(alpha)
                v.icon.overlay:SetAlpha(alpha)
                v:SetAlpha(alpha)
            end
            
            numRings = numRings + 1
        end
    end,
    
    OnSync = function(self, ...)
        for k,v in self._items do
            self:OnSyncItemUpdate(v, unpack(arg))
        end
    end,
    
    # Left intentionally blank; for inheritance
    OnSyncItemUpdate = function(self, item, ...)
    end,
}